class VolunteerTest extends grails.util.WebTest {

    // Unlike unit tests, functional tests are often sequence dependent.
    // Specify that sequence here.
    void suite() {
        testVolunteerListNewDelete()
        // add tests for more operations here
    }

    def testVolunteerListNewDelete() {
        webtest('Volunteer basic operations: view list, create new entry, view, edit, delete, view') {
            
            invoke      'volunteer'
            verifyText  'Home'

            verifyListSize 0

            clickLink   'New Volunteer'
            verifyText  'Create Volunteer'
            setInputField(name:'firstName', 'Volunteer1FN')
            setInputField(name:'lastName', 'Volunteer1LN')
            setInputField(name:'email', 'Volunteer1@email.org')
            setInputField(name:'password', 'v1')
            clickButton 'Create'
            verifyText  'Show Volunteer', description:'Detail page'
            clickLink   'List', description:'Back to list view'

            verifyListSize 1

            // We added VolunteerFilter so need to login
            // Login
            clickLink   'Login'
            setSelectField name:'eMail', value:'Volunteer1@email.org'
            clickButton 'Login'
            clickLink 'Home'
            invoke 'user'


            group(description:'edit the one element') {
                showFirstElementDetails()
                clickButton 'Edit'
                verifyText  'Edit Volunteer'
                clickButton 'Update'
                verifyText  'Show Volunteer'
                clickLink   'List', description:'Back to list view'
            }

            verifyListSize 1

            group(description:'delete the only element') {
                showFirstElementDetails()
                clickButton 'Delete'
                verifyXPath xpath:  "//div[@class='message']",
                            text:   /.*Volunteer.*deleted.*/,
                            regex:  true
            }

            verifyListSize 0
        }
    }

    String ROW_COUNT_XPATH = "count(//div[@class='list']//tbody/tr)"

    def verifyListSize(int size) {
        ant.group(description:"verify Volunteer list view with $size row(s)") {
            verifyText  'Volunteer List'
            verifyXPath xpath:      ROW_COUNT_XPATH,
                        text:       size,
                        description:"$size row(s) of data expected"
        }
    }

    def showFirstElementDetails() {
        ant.clickLink   '2', description:'go to detail view'
    }
}